package A2;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

/**
 * <p>Title: Class to handle Widget Order GUI</p>
 * <p>Description: CS 343 Assignment #2</p>
 * @author Mike Harmon
 * @author Erik Olson
 * @author Casey Golla
 * @author Jack Freiermuth
 * @email michael.harmon@my.uwrf.edu
 * @email erik.olson@my.uwrf.edu
 * @email casey.golla@my.uwrf.edu
 * @email jack.freiermuth@my.uwrf.edu
 * @date November 4th 2013
 * @team 5
 */ 
public class WidgetOrder extends JFrame {

	private static final long serialVersionUID = 1L;
	private OrderOfWidgets order;
	private static List<Widget> widgetInventory;
	private static Vector<String> widgetStringVector = new Vector<String>();
	private JList<String> listView;
	private JScrollPane scrollPane;
	private JSpinner widget_1_total;
	private JSpinner widget_2_total;
	private JSpinner widget_3_total;
	private JSpinner widget_4_total;
	private JSpinner widget_5_total;
	private JSpinner widget_6_total;
	private JSpinner widget_7_total;
	private JSpinner widget_8_total;
	private JSpinner widget_9_total;
	private JSpinner widget_a_total;
	private JButton send_order;
	
	public WidgetOrder( final Vector<String> input_widgets ) {
		//set the size of the window
		setBounds( 200, 200, 575, 300 );
		
		setLayout( null );
		
		/*
		 * Set up a JList object to display a list 
		 */
		listView = new JList<String>( input_widgets );
		
		//adding scroll bars to the list
		scrollPane = new JScrollPane( listView );
		scrollPane.setBounds(5, 5, 475, 175);
		add( scrollPane );
		
		//creating spinners that have a minimum value of 0, max of 100, and increment by 1
		SpinnerNumberModel model1 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel model2 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel model3 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel model4 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel model5 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel model6 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel model7 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel model8 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel model9 = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		SpinnerNumberModel modela = new SpinnerNumberModel(0.0, 0.0, 100.0, 1.0);
		
		//adding spinners to the frame
		widget_1_total = new JSpinner(model1);
		widget_1_total.setBounds( 500, 5, 50, 15 );
		add( widget_1_total );
		
		widget_2_total = new JSpinner(model2);
		widget_2_total.setBounds( 500, 21, 50, 15 );
		add( widget_2_total );
		
		widget_3_total = new JSpinner(model3);
		widget_3_total.setBounds( 500, 38, 50, 15 );
		add( widget_3_total );
		
		widget_4_total = new JSpinner(model4);
		widget_4_total.setBounds( 500, 56, 50, 15 );
		add( widget_4_total );
		
		widget_5_total = new JSpinner(model5);
		widget_5_total.setBounds( 500, 74, 50, 15 );
		add( widget_5_total );
		
		widget_6_total = new JSpinner(model6);
		widget_6_total.setBounds( 500, 92, 50, 15 );
		add( widget_6_total );
		
		widget_7_total = new JSpinner(model7);
		widget_7_total.setBounds( 500, 110, 50, 15 );
		add( widget_7_total );
		
		widget_8_total = new JSpinner(model8);
		widget_8_total.setBounds( 500, 128, 50, 15 );
		add( widget_8_total );
		
		widget_9_total = new JSpinner(model9);
		widget_9_total.setBounds( 500, 146, 50, 15 );
		add( widget_9_total );
		
		widget_a_total = new JSpinner(modela);
		widget_a_total.setBounds( 500, 164, 50, 15 );
		add( widget_a_total );
		
		//adding a "Send Order" button to the frame
		send_order = new JButton( "Send Order" );
		send_order.addActionListener( new ActionListener(){
			/*
			 * This method is executed each time the button is clicked.
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent e) {
				//getting the values from all of the spinners
				double widget_1_value = (double) widget_1_total.getValue();
				double widget_2_value = (double) widget_2_total.getValue();
				double widget_3_value = (double) widget_3_total.getValue();
				double widget_4_value = (double) widget_4_total.getValue();
				double widget_5_value = (double) widget_5_total.getValue();
				double widget_6_value = (double) widget_6_total.getValue();
				double widget_7_value = (double) widget_7_total.getValue();
				double widget_8_value = (double) widget_8_total.getValue();
				double widget_9_value = (double) widget_9_total.getValue();
				double widget_a_value = (double) widget_a_total.getValue();
				
				//boolean to determine if all spinners are 0
				boolean all_zero = false;
				
				//check if all values are 0
				if( widget_1_value < 1 && widget_2_value < 1 && widget_3_value < 1 && widget_4_value < 1 && widget_5_value < 1 &&
					widget_6_value < 1 && widget_7_value < 1 && widget_8_value < 1 && widget_9_value < 1 && widget_a_value < 1  ) {
					all_zero = true;
				}
				
				//if all values are 0 inform that they need at least 1 widget, else send order
				if( true == all_zero ) {
					JOptionPane.showMessageDialog(null, "Need to order at least 1 widget");
				} else {
					order = new OrderOfWidgets( widget_1_value, widget_2_value, widget_3_value, widget_4_value, widget_5_value, widget_6_value, widget_7_value, widget_8_value, widget_9_value, widget_a_value );
					
					sendOrder( order );
					
					JOptionPane.showMessageDialog(null, "Order Sent");
				}
			}
				
		});
		send_order.setBounds(225, 200, 125, 50);
		add(send_order);
		
		//set frame to be visible
		setVisible( true );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
	}

	/**
	 * Function to populate widgetStringVector with the widget information
	 */
	public static void getWidgets() {
		//populating widgetStringVector with widget information
		for (Widget w : widgetInventory) {
			widgetStringVector.add(w.toString());
		}

		//getting a unique set of widgets
		Set<String> unique = new HashSet<String>();
		unique.addAll(widgetStringVector);
		widgetStringVector.clear();
		widgetStringVector.addAll(unique);
		
		//sorting widget information
		Collections.sort( widgetStringVector );
	}
	
	/**
	 * Function to send an order
	 * @param order
	 */
	public void sendOrder(OrderOfWidgets order) {
		final String PERSISTENCE_UNIT_NAME = "WidgetsAndOrders";
		EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		
	    EntityManager em = factory.createEntityManager();

	    // create new order to persist into the db
	    em.getTransaction().begin();
	    
	    em.persist(order);
	    
	    em.getTransaction().commit();
	    
	    em.close();
	}
	
	public static void main(String[] args) {	    
	    final String PERSISTENCE_UNIT_NAME = "WidgetsAndOrders";
		EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		
	    EntityManager em = factory.createEntityManager();

	    // create new widget to persist into the db
	    em.getTransaction().begin();
	    	    
	    // Get the entire Inventory of Widgets from the database
	    Query q = em.createQuery("select w from Widget w");
	    widgetInventory = q.getResultList();
	    em.close();
	    
	    //populating vector with widgets
	    getWidgets();
	    
	    //launching view
	    new WidgetOrder( widgetStringVector );
	   
	}
}